package system.security.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import system.security.dao.ResourceDao;
import system.security.entity.Resource;
import system.security.service.ResourceService;


@Service("resourceService")
public class ResourceServiceImpl implements ResourceService<Resource> {

	@Autowired
	ResourceDao resourceDao;
	
	@Transactional(readOnly=true)
	@Override
	public Resource getById(int resourceId) {
		Resource resource = resourceDao.get(resourceId);
		return resource;
	}

	@Override
	public Resource findByName(String resourcename) {
			List<Resource> list = resourceDao.findByProperty("resourcename", resourcename);
			if(!list.isEmpty()){
				return list.get(0);
			}
			return null;		
	}

	@Override
	@Transactional(readOnly = true)
	public List<Resource> getAll() {
		return resourceDao.list();
	}
	
	@Override
	public void save(Resource resource) throws Exception {
			resourceDao.saveOrUpdate(resource);
	}

	@Override
	public void delete(int resourceId) {
		resourceDao.remove(resourceId);
	}
	
	@Override
	public void delete(Resource resource){
		resourceDao.remove(resource);		
	}

	
}
